package tienmanh.gs.goldminer.goldsoft;

import android.view.KeyEvent;
import java.io.IOException;
import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.ZoomCamera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.AudioOptions;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.IGameInterface;
import org.andengine.ui.IGameInterface.OnCreateResourcesCallback;
import org.andengine.ui.IGameInterface.OnCreateSceneCallback;
import org.andengine.ui.IGameInterface.OnPopulateSceneCallback;
import org.andengine.ui.activity.BaseGameActivity;

import tienmanh.gs.goldminer.manager.ResourcesManager;
import tienmanh.gs.goldminer.manager.SceneManager;

public class GameActivity extends BaseGameActivity
{
  private ZoomCamera camera;

  public Engine onCreateEngine(EngineOptions paramEngineOptions)
  {
    return new LimitedFPSEngine(paramEngineOptions, 60);
  }

  public EngineOptions onCreateEngineOptions()
  {
    this.camera = new ZoomCamera(0.0F, 0.0F, 800.0F, 480.0F);
    EngineOptions localEngineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), this.camera);
    localEngineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
    localEngineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
    return localEngineOptions;
  }


  protected void onDestroy()
  {
    super.onDestroy();
    System.exit(0);
  }

  public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent)
  {
    if (paramInt == 4)
      SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
    return false;
  }


@Override
public synchronized void onResumeGame() {
	// TODO Auto-generated method stub
	super.onResumeGame();
	SceneManager.getInstance().getCurrentScene().onResumeGame();
}

//@Override
//public synchronized void onPauseGame() {
//	// TODO Auto-generated method stub
////	super.onPauseGame();
//	SceneManager.getInstance().getCurrentScene().onPauseGame();
//}



@Override
protected void onPause() {
	// TODO Auto-generated method stub
	super.onPause();
	SceneManager.getInstance().getCurrentScene().onPauseGame();
}

@Override
public void onCreateResources(
		OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
	// TODO Auto-generated method stub
	 ResourcesManager.prepareManager(this.mEngine, this, this.camera, getVertexBufferObjectManager());
	 pOnCreateResourcesCallback.onCreateResourcesFinished();
}

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
		throws Exception {
	// TODO Auto-generated method stub
	 SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
}

@Override
public void onPopulateScene(Scene pScene,
		OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
	// TODO Auto-generated method stub
	this.mEngine.registerUpdateHandler(new TimerHandler(2.0F, new ITimerCallback()
    {
      public void onTimePassed(TimerHandler paramTimerHandler)
      {
        GameActivity.this.mEngine.unregisterUpdateHandler(paramTimerHandler);
      }
    }));
    pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}

